{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "THE FINAL RESULT IS:  The final answer is:\n",
      "\n",
      "\\((n^2/2) \\times (n^2/2 - n) \\times (n^2/2 - 2n) \\times ... \\times (n^2/2 - (n-1)n)\\)\n"
     ]
    }
   ],
   "source": [
    "import logging\n",
    "import json\n",
    "import os\n",
    "import sys\n",
    "from tqdm import tqdm\n",
    "\n",
    "sys.path.append(os.getcwd())\n",
    "\n",
    "from CHAMP_utils import *\n",
    "\n",
    "setOpenAi(keyid = 3)\n",
    "\n",
    "GPT_MODEL = \"gpt-3.5-turbo\"\n",
    "\n",
    "\n",
    "file_path = '/Users/natehu/Desktop/Tsinghua Research/Tsinghua TaDe/ToT复现/TOT/champ_116.json'\n",
    "with open(file_path, 'r', encoding='utf-8') as file:\n",
    "    dataset = json.load(file)\n",
    "\n",
    "success_Q = 0\n",
    "unsuccess_Q = 0\n",
    "error_Q = 0\n",
    "question_ids = range(len(dataset))\n",
    "\n",
    "question_id = 0\n",
    "\n",
    "question = dataset[question_id]['problem_text']\n",
    "type = split_string_by_(dataset[question_id]['problem_identifier'])\n",
    "gold_answer = dataset[question_id]['problem_answer']\n",
    "\n",
    "\n",
    "decompose_steps = decompose_sql(question, type, GPT_MODEL)\n",
    "steps, steps_dict = convert_steps_to_format(decompose_steps)\n",
    "num_steps = len(steps)\n",
    "\n",
    "\n",
    "solution = []\n",
    "\n",
    "sys_q = f\"\"\"There is a math problem. I need you to solve it and give an answer.\n",
    "Here is the problem:\\n{question}\n",
    "\n",
    "I have broken this math problem down into a series of smaller problems. I will assign you sub-problems one by one, and provide the results of the previous sub-problems as a reference for your reasoning.\n",
    "Please solve the problem and respond according to mathematical logic.\n",
    "\"\"\"  \n",
    "\n",
    "for i in range(num_steps):\n",
    "    subtask = steps[i]\n",
    "    subask = f\"\"\"\\nThe sub-problem to solve now is: {subtask}\n",
    "Based on the information above, please provide a concise and clear answer\"\"\"\n",
    "    \n",
    "    if len(solution) == 0:\n",
    "        query = subask\n",
    "        Q = [{'role':'system', 'content':sys_q},\n",
    "                {'role':'user', 'content':query}]\n",
    "    else:\n",
    "        answersSoFar = f\"\"\"\\nSo far, the answers to the preceding sub-problems are as follows: The format is Sub-problem-Id: xxx; Sub-problem: xxx; Answer: xxx.\"\"\"\n",
    "        for index, value in enumerate(solution):\n",
    "            answersSoFar += f\"\"\"\\nSub-problem-Id: {index}; Sub-problem: {steps[index]}; Answer: {value}.\"\"\"\n",
    "        query = answersSoFar + subask\n",
    "        Q = [{'role':'system', 'content':sys_q},\n",
    "                {'role':'user', 'content':query}]\n",
    "\n",
    "    result = askChatGPT(Q, model=GPT_MODEL, temperature=1)\n",
    "    solution.append(result)\n",
    "\n",
    "user_q = f\"\"\"There is a math problem:\\n{question}\n",
    "\n",
    "I have broken this math problem down into a series of smaller problems and each sub-problem is solved.\n",
    "The sub-problems and their corresponding answers are as follows. (Format: Sub-problem-Id: xxx; Sub-problem: xxx; Answer: xxx.)\"\"\"\n",
    "\n",
    "for index, value in enumerate(solution):\n",
    "    user_q += f\"\"\"\\nSub-problem-Id: {index}; Sub-problem: {steps[index]}; Answer: {value}.\"\"\"\n",
    "\n",
    "Q = [{'role':'system', 'content':sys_q},\n",
    "        {'role':'user', 'content':user_q},\n",
    "        {'role':'user', 'content':f\"\"\"Now that all the sub-problems have been solved, what is the final answer?\n",
    "Please give the final answer without any additional explanation or clarification.\"\"\"}]\n",
    "\n",
    "finalResult = askChatGPT(Q, model=GPT_MODEL, temperature=1)\n",
    "print(\"THE FINAL RESULT IS: \", finalResult)\n",
    "\n",
    "judgeAnswer = {'role':'user', 'content':f\"\"\"Here is a math problem with a standard answer and a student's solution. Please help me determine if the student's solution is correct.\n",
    "Problem: {question}\n",
    "\n",
    "Standard answer: {gold_answer}\n",
    "\n",
    "Answer: {finalResult}\n",
    "\n",
    "If the student's answer is correct, just output True; otherwise, just output False.\n",
    "No explanation is required.\n",
    "\"\"\"}\n",
    "\n",
    "Q_judge = [judgeAnswer]\n",
    "ifcorrect = askChatGPT(Q_judge, model=GPT_MODEL, temperature=1)\n",
    "\n",
    "\n",
    "if 'True' in ifcorrect:\n",
    "    success_Q += 1\n",
    "\n",
    "elif 'False' in ifcorrect:\n",
    "    unsuccess_Q += 1\n",
    "\n",
    "else:\n",
    "    error_Q += 1\n",
    "\n",
    "\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['The first rook can be placed in \\\\(n^2/2\\\\) ways without conflicting with any other rooks.',\n",
       " 'Sub-problem-Id: 1; Sub-problem: How many ways can the second rook be placed on the chess board without conflicting with any other rooks?; Answer: After the first rook is placed, there will be \\\\(n^2/2 - n\\\\) squares left in the same row and column as the first rook. Therefore, the second rook can be placed in \\\\(n^2/2 - n\\\\) ways without conflicting with any other rooks.',\n",
       " 'Given that the first rook is placed, there are \\\\(n^2/2 - n\\\\) squares left in the same row and column as the first rook.\\nAfter the second rook is placed, there will be \\\\((n^2/2 - n) - n\\\\) squares left in the same rows and columns as the first two rooks for the third rook to be placed without conflicts.\\nTherefore, the third rook can be placed in \\\\((n^2/2 - n) - n = n^2/2 - 2n\\\\) ways without conflicting with any other rooks.',\n",
       " 'Sub-problem-Id: 3; Sub-problem: How many ways can the fourth rook be placed on the chess board without conflicting with any other rooks?; Answer: After placing the first three rooks, there are \\\\(n^2/2 - 3n\\\\) squares left in the same row and column as the first three rooks. Therefore, the fourth rook can be placed in \\\\(n^2/2 - 3n\\\\) ways without conflicting with any other rooks.',\n",
       " 'Sub-problem: How many ways can the nth rook be placed on the chess board without conflicting with any other rooks?\\nAnswer: After placing the first (n-1) rooks, there are \\\\(n^2/2 - (n-1)n\\\\) squares left in the same rows and columns as the first (n-1) rooks for the nth rook to be placed. Therefore, the nth rook can be placed in \\\\(n^2/2 - (n-1)n\\\\) ways without conflicting with any other rooks.',\n",
       " 'To determine the total number of ways n rooks can be placed on the n x n chess board peacefully, we need to find the product of the number of ways each rook can be placed without conflicting with any other rooks.\\n\\nTherefore, the total number of ways n rooks can be placed peacefully on an n x n chess board, invariant to a 180-degree rotation, is given by:\\n\\n\\\\((n^2/2) \\\\times (n^2/2 - n) \\\\times (n^2/2 - 2n) \\\\times ... \\\\times (n^2/2 - (n-1)n)\\\\).']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'role': 'system',\n",
       "  'content': 'There is a math problem. I need you to solve it and give an answer.\\nHere is the problem:\\nOn a chess board, two rooks are placed peacefully if they are not on the same row or column. For an n x n chess board, find the number of ways that n rooks can be placed peacefully (i.e., any two are placed peacefully) and the placing is also invariant to a 180-degree rotation.\\n\\nI have broken this math problem down into a series of smaller problems. I will assign you sub-problems one by one, and provide the results of the previous sub-problems as a reference for your reasoning.\\nPlease solve the problem and respond according to mathematical logic.\\n'},\n",
       " {'role': 'user',\n",
       "  'content': 'There is a math problem:\\nOn a chess board, two rooks are placed peacefully if they are not on the same row or column. For an n x n chess board, find the number of ways that n rooks can be placed peacefully (i.e., any two are placed peacefully) and the placing is also invariant to a 180-degree rotation.\\n\\nI have broken this math problem down into a series of smaller problems and each sub-problem is solved.\\nThe sub-problems and their corresponding answers are as follows. (Format: Sub-problem-Id: xxx; Sub-problem: xxx; Answer: xxx.)\\nSub-problem-Id: 0; Sub-problem: How many ways can the first rook be placed on the chess board without conflicting with any other rooks?; Answer: The first rook can be placed in \\\\(n^2/2\\\\) ways without conflicting with any other rooks..\\nSub-problem-Id: 1; Sub-problem: How many ways can the second rook be placed on the chess board without conflicting with any other rooks?; Answer: Sub-problem-Id: 1; Sub-problem: How many ways can the second rook be placed on the chess board without conflicting with any other rooks?; Answer: After the first rook is placed, there will be \\\\(n^2/2 - n\\\\) squares left in the same row and column as the first rook. Therefore, the second rook can be placed in \\\\(n^2/2 - n\\\\) ways without conflicting with any other rooks..\\nSub-problem-Id: 2; Sub-problem: How many ways can the third rook be placed on the chess board without conflicting with any other rooks?; Answer: Given that the first rook is placed, there are \\\\(n^2/2 - n\\\\) squares left in the same row and column as the first rook.\\nAfter the second rook is placed, there will be \\\\((n^2/2 - n) - n\\\\) squares left in the same rows and columns as the first two rooks for the third rook to be placed without conflicts.\\nTherefore, the third rook can be placed in \\\\((n^2/2 - n) - n = n^2/2 - 2n\\\\) ways without conflicting with any other rooks..\\nSub-problem-Id: 3; Sub-problem: ...; Answer: Sub-problem-Id: 3; Sub-problem: How many ways can the fourth rook be placed on the chess board without conflicting with any other rooks?; Answer: After placing the first three rooks, there are \\\\(n^2/2 - 3n\\\\) squares left in the same row and column as the first three rooks. Therefore, the fourth rook can be placed in \\\\(n^2/2 - 3n\\\\) ways without conflicting with any other rooks..\\nSub-problem-Id: 4; Sub-problem: How many ways can the nth rook be placed on the chess board without conflicting with any other rooks?; Answer: Sub-problem: How many ways can the nth rook be placed on the chess board without conflicting with any other rooks?\\nAnswer: After placing the first (n-1) rooks, there are \\\\(n^2/2 - (n-1)n\\\\) squares left in the same rows and columns as the first (n-1) rooks for the nth rook to be placed. Therefore, the nth rook can be placed in \\\\(n^2/2 - (n-1)n\\\\) ways without conflicting with any other rooks..\\nSub-problem-Id: 5; Sub-problem: Determine the total number of ways n rooks can be placed on the n x n chess board peacefully.; Answer: To determine the total number of ways n rooks can be placed on the n x n chess board peacefully, we need to find the product of the number of ways each rook can be placed without conflicting with any other rooks.\\n\\nTherefore, the total number of ways n rooks can be placed peacefully on an n x n chess board, invariant to a 180-degree rotation, is given by:\\n\\n\\\\((n^2/2) \\\\times (n^2/2 - n) \\\\times (n^2/2 - 2n) \\\\times ... \\\\times (n^2/2 - (n-1)n)\\\\)..'},\n",
       " {'role': 'user',\n",
       "  'content': 'Now that all the sub-problems have been solved, what is the final answer?\\nPlease give the final answer without any additional explanation or clarification.'}]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
